package com.atguigu.srb.core.controller.Api;

import com.atguigu.common.result.R;
import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.core.pojo.vo.BorrowerVO;
import com.atguigu.srb.core.service.BorrowerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * @ClassName BorrowerController
 * @Author ZengRJ
 * @Date 2022/11/1 19:18
 * @Version 1.0
 */
@Api(tags = "借款人")
@RestController
@RequestMapping("/api/core/borrower")
@Slf4j
public class BorrowerController {

    @Resource
    BorrowerService borrowerService;

    @ApiOperation("保存借款人信息")
    @PostMapping("/auth/save")
    public R save(@RequestBody BorrowerVO borrowerVO, HttpServletRequest request) {
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        System.out.println(borrowerVO);
        borrowerService.saveBorrowerVOByUserId(borrowerVO, userId);
        return R.ok().message("信息提交成功");
    }

    @ApiOperation("获取借款人状态")
    @GetMapping("/auth/getBorrowerStatus")
    public R getBorrowerStatus(HttpServletRequest request) {
        String token = request.getHeader("token");
        log.info("接收到token:{}",token);
        Long userId = JwtUtils.getUserId(token);
        Integer status = borrowerService.getStatusByUserId(userId);
        return R.ok().data("borrowerStatus", status);
    }

    @ApiOperation("获取借款额度")
    @GetMapping("/auth/getBorrowerAmount")
    public R getBorrowerAmount(HttpServletRequest request) {
        String token = request.getHeader("token");
        log.info("接收到token:{}",token);
        Long userId = JwtUtils.getUserId(token);
        BigDecimal borrowerAmount = borrowerService.getBorrowerAmount(userId);
        return R.ok().data("borrowerAmount", borrowerAmount);
    }

}
